<?php
defined('SYS_RUN') or exit();

class Script {

    const DIR_CSS = 'static/css/';

    const DIR_JS = 'static/js/';

    const DIR_LESS = 'static/less/';

    const SUB_DIR = 'assets/';

    const STYLE_CACHED_FILE = 'style.css';

    const SCRIPT_CACHED_FILE = 'script.js';

    private $_files = array();

    private $_content = array(
        'script'            => '',
        'stylesheet'    => ''
    );

    private $_rebuild;

    private $_compress;

    private $_cachedFiles = array();

    function __construct() {
        $this->_files = Settings::get('script');

        Lib::get('file')->mkdir(DIR_CACHE.self::SUB_DIR, 777);

        $this->_cachedFiles = array(
            'style' => array(
                'file'  => DIR_CACHE.self::SUB_DIR.self::STYLE_CACHED_FILE,
                'url'   => URL_CACHE.self::SUB_DIR.self::STYLE_CACHED_FILE
            ),
            'script' => array(
                'file'  => DIR_CACHE.self::SUB_DIR.self::SCRIPT_CACHED_FILE,
                'url'   => URL_CACHE.self::SUB_DIR.self::SCRIPT_CACHED_FILE
            )
        );

        $rebuild = Lib::get('request')->get('staticRebuild');

        if($rebuild
            || !file_exists($this->_cachedFiles['style']['file'])
            || !file_exists($this->_cachedFiles['script']['file']) ) {
            $this->_rebuild = true;
            $this->_filesCaching();
        } else
            $this->_rebuild = $this->_files['rebuild'];

        $this->_compress = $this->_files['compress'];


        if($this->_rebuild) {
            $this->_filesCaching();
        } else {
            $this->_readCachedContent();
        }
    }

    public function registerCss($file) {
        $this->_files['css'][] = $file;
    }

    public function registerJs($file) {
        $this->_files['js'][] = $file;
    }

    public function registerLess($file) {
        $this->_files['less'][] = $file;
    }

    public function getCachedFilesHtml() {
        $files = $this->get_cachedFiles();
        return array(
            '<script type="text/javascript" src="'.$files['script']['url'].'"></script>',
            '<link rel="stylesheet" type="text/css" href="'.$files['style']['url'].'" />'
        );
    }

    public function get_cachedFiles() {
        return $this->_cachedFiles;
    }

    public function getJsContent() {
        return $this->_content['script'];
    }

    public function getStylesheetContent() {
        return $this->_content['stylesheet'];
    }

    private function _filesCaching() {
        $this->_truncate_cachedFiles();
        $this->_buildCacheFile();
    }

    private function _buildCacheFile() {
        foreach($this->_files as $type => $files) {
            if(is_array($files))
                foreach($files as $file) {
                    $this->_addToCacheFile($type, $file);
                }
        }
    }

    private function _addToCacheFile($type, $file) {
        $oFile = Lib::get('file');
        $oCompress = Lib::get('compress');
        $content = '';
        switch($type) {
            case 'js':
                $fullPath = DIR_APPLICATION.self::DIR_JS.$file;
                $content = file_get_contents($fullPath);
                if($this->_compress) $content = $oCompress->compressJs($content);
                $this->_prepareContent($content);
                $this->_content['script'] .= $content;
                $oFile->open($this->_cachedFiles['script']['file']);
                break;
            case 'css':
                $fullPath = DIR_APPLICATION.self::DIR_CSS.$file;
                $content = file_get_contents($fullPath);
                if($this->_compress) $content = $oCompress->compressCss($content);
                $this->_prepareContent($content);
                $this->_content['stylesheet'] .= $content;
                $oFile->open($this->_cachedFiles['style']['file']);
                break;
            case 'less':
                $fullPath = DIR_APPLICATION.self::DIR_LESS.$file;
                $content = Lib::get('less')->compileFile($fullPath);
                if($this->_compress) $content = $oCompress->compressCss($content);
                $this->_prepareContent($content);
                $this->_content['stylesheet'] .= $content;
                $oFile->open($this->_cachedFiles['style']['file']);
                break;
        }
        $oFile->write($content, false)
            ->close();
    }

    private function _truncate_cachedFiles() {
        $oFile = Lib::get('file');
        $oFile->open($this->_cachedFiles['style']['file'])
            ->truncate()
            ->close();
        $oFile->open($this->_cachedFiles['script']['file'])
            ->truncate()
            ->close();
    }

    private function _readCachedContent() {
        $oFile = Lib::get('file');
        $this->_content['js'] = $oFile->open($this->_cachedFiles['script']['file'])
            ->read();
        $this->_content['stylesheet'] = $oFile->open($this->_cachedFiles['style']['file'])
            ->read();
        $oFile->close();
    }

    private function _prepareContent(&$content) {
        $content = $content.PHP_EOL;
    }
}